<?php
   class Cart extends MY_Controller{
	   public function __construct(){
		   parent::__construct();
		   $this->load->helper("url");
		   $this->load->library('cart');
		   $this->load->helper('form');
		   $this->load->model("model_cart");
		   $this->load->model("model_home");
		   //$this->load->model("model_cruise");
	   }
	   public function index(){
		   $this->debug($this->cart->contents());
		   $this->load->view("public/tickets"); 
	   }
	   public function add(){
		   if(!$_POST){die("false");}
		   $id = $_POST['id']; $type = $_POST['type'];
		   $val = $_POST['val']; $name = $_POST['name'];
		   $train = $_POST['trainame'];$start = $_POST['start'];
		   $from = $_POST['from']; $to = $_POST['to'];
		   $end = $_POST['end'];
		   $data = $this->model_cart->gettick($id);
		   $shop = array(
			   'id'      => $id,
			   'qty'     => 1,
			   'price'   => $val,
			   'name'    => $name,
			   'options' => array(
			   		'trainid' => $data['trainid'],
			   	    'train' => $train,
					'start' => $start,
					'end' => $end,
					'from' => $from,
					'to'=>$to,
					'type'  => $type,
				)
			);
			$this->cart->insert($shop);	
	   }
	   public function addorther(){
		   if(!$_POST){die("false");}
		   $id = $_POST['id']; $type = $_POST['type'];
		   $val = $_POST['val']; $name = $_POST['name'];
		   $train = $_POST['train'];$start = $_POST['start'];
		   $from = $_POST['from']; $to = $_POST['to'];
		   $end = $_POST['end'];
		   $data = $this->model_cart->gettickorther($id);
		   $shop = array(
			   'id'      => $id,
			   'qty'     => 1,
			   'price'   => $val,
			   'name'    => $name,
			   'numchild' => 0,
			   'pricechild' => 0,
			   'options' => array(
			   		'trainid' => $data['trainid'],
			   	    'train' => $train,
					'start' => $start,
					'end' => $end,
					'from' => $from,
					'to'=>$to,
					'type'  => $type,
					'startdate' => date('d/m/Y')
				)
			);
			$this->cart->insert($shop);	
	   }
	   public function view(){
		  $data['cart'] = $this->cart->contents();
		  if($data['cart'] == NULL){ redirect(base_url());}
		  foreach($data['cart'] as $k => $v){
			  $service = $this->model_cart->getservice($v['options']['trainid']);
			  if($service != NULL){
				$data['cart'][$k]['options']['price'] = $service['service_child'];
			  }else{
				$data['cart'][$k]['options']['price'] = 0;
			  }
		  }
		  $data['abouts'] = $this->abouts();
		  $data['listtrains'] = $this->listtrains(20);
		  $data['destinations'] = $this->destinations(6);
		  $data['listregions'] = $this->listregions();
		  $data['listquestion'] = $this->faqs();
		  //$this->debug($data['cart']);
		  $data['title'] = "Your cart - trains in Vietnam";
		  $this->load->view("cart/layout",$data);	   
	   }
	   public function updatecart(){
		    if(!$_POST){die("false");}
			for($i=1;$i<=$this->cart->total_items();$i++){
			    $rowid = $_POST['rowid'];
				$data = array(
						'rowid'  => $rowid,
						'qty' => $_POST['number'],
						'numchild' => $_POST['child'],
						'pricechild' => $_POST['service'],
						//'options' => array('startdate')
				);
				$this->cart->update($data);
			}
			/*for($i=1;$i<=$this->cart->total_items();$i++){
		        $item = $this->input->post($i);
		        $data = array(
		                'rowid'    => $item['rowid'],
		                'qty'    => $item['qty']
				);
		        $this->cart->update($data);
			}
			*/
			//redirect(base_url().'home/cart/view','refresh');		
		}
	   public function del(){
		  	$getshop=$this->cart->contents();
			foreach($getshop as $item){
				if($item['id'] == $_POST['id']){
					$data['rowid']=$item['rowid'];
					$data['qty'] = 0;
			        $this->cart->update($data);
					break;
				}
			}
			//redirect(base_url().'your-cart#infocruise','refresh');	
	   }
	   public function delone(){
		  	$getshop=$this->cart->contents();
			$id = (int)$this->uri->segment(4);
			foreach($getshop as $item){
				if($item['id'] == $id){
					$data['rowid']=$item['rowid'];
					$data['qty'] = 0;
			        $this->cart->update($data);
					break;
				}
			}
			redirect(base_url().'checkout/bookinginfo','refresh');	
	   }
	   public function emptycart(){
		   $this->cart->destroy();	
		   redirect(base_url()."home/cart");
	   }
	   public function emptys(){
		   $this->cart->destroy();
	   }
   }